<?php

App::uses('AdminAppController', 'Admin.Controller');

/**
 * Comments Controller
 *
 * @property Comment $Comment
 */
class CommentsController extends AdminAppController {
	
	public $uses = array("Comment");

	public function index() {
		$this->Comment->recursive = 0;
		$comments = $this->paginate();
		$this->set(compact('comments'));
	}

	public function view($id = null) {
		$this->Comment->id = $id;
		if (!$this->Comment->exists()) {
			$this->setUserInfo(__("Invalid comment."), "error");
			$this->redirect( $this->referer() );
		}
		$this->set('comment', $this->Comment->read(null, $id));
	}

	public function add() {
		if ($this->request->is('post')) {
			try {
				// begin transaction
				$this->Comment->getDataSource()->begin();
				$this->Comment->create();
				if ($this->Comment->save($this->request->data)) {
					$this->setUserInfo( __("The comment has been saved."), "success" );
					$this->redirect(array('action' => 'index'));
				}
				else {
					throw new Exception( __("The comment could not be saved.") );
				}
			}
			catch(Exception $ex) {
				// rollback transaction
				$this->Comment->getDataSource()->rollback();
				// handle the exception
				$this->log("Error saving comment: " . $ex->getMessage());
				$this->setUserInfo( __("The comment could not be saved: ") . $ex->getMessage(), 'error');
			}
		}
		
		$spots = $this->Comment->Spot->find('list');
		$users = $this->Comment->User->find('list');
		$this->set(compact('spots', 'users'));
	}


	public function edit($id = null) {
	
		$this->Comment->id = $id;
		
		if (!$this->Comment->exists()) {
			$this->setUserInfo(__("Invalid comment."), "error");
			$this->redirect( $this->referer() );
		}
		
		if ($this->request->is('post') || $this->request->is('put')) {
			try {
				// begin transaction
				$this->Comment->getDataSource()->begin();
				if ($this->Comment->save($this->request->data)) {
					$this->setUserInfo( __("The comment has been saved."), "success" );
					$this->redirect(array('action' => 'view', $this->Comment->id));
				}
				else {
					throw new Exception( __("The comment could not be saved.") );
				}
			}
			catch(Exception $ex) {
				// rollback transaction and handle the exception
				$this->Comment->getDataSource()->rollback();
				$this->log("Error saving comment: " . $ex->getMessage());
				$this->setUserInfo( __("The comment could not be saved: ") . $ex->getMessage(), 'error');
			}
		}
		else {
			$this->request->data = $this->Comment->read(null, $id);
		}
		
		$spots = $this->Comment->Spot->find('list');
		$users = $this->Comment->User->find('list');
		$this->set(compact('spots', 'users'));
	}

	public function delete($id = null) {
		
		$this->Comment->id = $id;
		
		if (!$this->Comment->exists()) {
			$this->setUserInfo(__("Invalid comment."), "error");
			$this->redirect( $this->referer() );
		}
		
		if ($this->Comment->delete()) {
			$this->setUserInfo( __("Comment deleted."), 'success' );
			$this->redirect(array('action' => 'index'));
		}
		
		$this->setUserInfo( __("Comment was not deleted."), 'error' );
		$this->redirect(array('action' => 'index'));
	}
	
}
